package cn.ljlao.mall.product.goods.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.ljlao.common.utils.PageUtils;
import cn.ljlao.common.utils.R;
import cn.ljlao.mall.product.goods.entity.ProductAttrValueEntity;
import cn.ljlao.mall.product.goods.service.ProductAttrValueService;
import cn.ljlao.mall.product.goods.vo.AttrRspVo;
import cn.ljlao.mall.product.goods.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.ljlao.mall.product.goods.service.AttrService;


/**
 * 商品属性
 *
 * @author LiuZhen
 * @email ljlao@vip.qq.com
 * @date 2020-10-14 13:51:59
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    ///product/attr/base/list/{catelogId}
    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){
       List<ProductAttrValueEntity> list =  productAttrValueService.listforspu(spuId);
        return R.ok().put("data",list);
    }

    ///product/attr/update/{spuId}
    /**
     * 修改
     */
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("goods:attr:update")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
            @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }


    /**
     * 列表
     */
    //@RequestMapping("/base/list/{catelogId}")
    //attr/sale/list
    @RequestMapping("/{attr}/list/{catelogId}")
    //@RequiresPermissions("goods:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attr") String type){
        PageUtils page = attrService.baseList(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("goods:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("goods:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrRspVo attrRspVo = attrService.getInfo(attrId);
        return R.ok().put("attr", attrRspVo);

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("goods:attr:save")
    public R save(@RequestBody AttrVo attr){

		attrService.saveDetails(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("goods:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateByDetial(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("goods:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
